//
//  MovieViewController.m
//  UI_APP_Project_Bean
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieTableViewCell.h"
#import "MovieModel.h"
#import "MovieDetiledViewController.h"
#import "CollectionViewController.h"

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *moviesArray;


@end

@implementation MovieViewController

- (void)dealloc
{
    [_moviesArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self config];
    [self handleData];
    [self creatTableView];
}


-(void)config{
    self.navigationItem.title = @"电影";
    
    self.moviesArray = [NSMutableArray array];
    self.navigationController.navigationBar.barTintColor =[UIColor colorWithRed:0.37 green:0.6 blue:0.7 alpha:1];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(collectionAction:)];
}

-(void)collectionAction:(UIBarButtonItem *)bar{
    
    CollectionViewController *collecVC = [[CollectionViewController alloc]init];
//    collecVC.collectionArray = [NSMutableArray array];
    collecVC.collectionArray = self.moviesArray;
    
    [self.navigationController pushViewController:collecVC animated:YES];
    
    
}


-(void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@".txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dic objectForKey:@"result"];
    
    for (NSDictionary *dic in array) {
        MovieModel *model = [[MovieModel alloc]init];
        
        [model setValuesForKeysWithDictionary:dic];
        [self.moviesArray addObject:model];
        
    }
    
    
    
}



-(void)creatTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    [self.view addSubview:tableView];
    [tableView release];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 180;
    
    [tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cellMovie"];
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.moviesArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellMovie"];
    
    
    
    cell.model = [self.moviesArray objectAtIndex:indexPath.row];
    
    return cell;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetiledViewController *movDetiVc = [[MovieDetiledViewController alloc]init];
    
    movDetiVc.movieModel = [self.moviesArray objectAtIndex:indexPath.row];
    
    
    [self.navigationController pushViewController:movDetiVc animated:YES];
    
    
    [movDetiVc release];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
